//
//  ST_RegistStepHelperController.swift
//  btvideo
//
//  Created by mac on 2021/3/3.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import WebKit
import SwiftyAttributes

//MARK: -
class ST_RegistHelperListController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title_nav_btn.setTitleForAllStates("帮助")
        
        view.backgroundColor = Color.white
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(own_navigation_bar.snp.bottom)
            make.bottom.right.left.equalToSuperview()
        }
        
    }
//    MARK: - Properties
    fileprivate lazy var itemWidth:CGFloat = {
        return ScreenWidth
    }()
    
    fileprivate lazy var itemHeight = 60.cgFloat
    
    fileprivate lazy var collectionView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize.init(width: itemWidth, height: itemHeight)
        
//        layout.headerReferenceSize = CGSize.init(width: ScreenWidth, height: 65)
        
        layout.minimumLineSpacing = 0
        //        layout.minimumInteritemSpacing = 14
        
        let bottomEdge = iPhoneXSeries ? BTSafeAreaBottom + 30 : 20 + 20
        
//        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 15, bottom: bottomEdge, right: 0)
        
        let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        
        collection.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: bottomEdge, right: 0)
        
        collection.register(cellWithClass: ST_OtherGeneralCell.self)
        collection.register(cellWithClass: ST_GeneralOfSwitchCell.self)
        
        
        collection.register(supplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withClass: SettingSectionHeader.self)
        
        collection.delegate = self
        
        collection.dataSource = self
        
        collection.alwaysBounceVertical = true
        
        collection.backgroundColor = Color.white
        
        if #available(iOS 11.0, *) {
            collection.contentInsetAdjustmentBehavior = .never
        }
        return collection
    }()
    
    fileprivate lazy var url_array:[URL] = {
        return [PH.phoneLoginHtml,PH.authCodeHtml,PH.wechatLogHtml,PH.qqLoginHtml].map({ URL(string: $0.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)) }).compactMap{$0}
    }()
    
    fileprivate lazy var data_array:[[ST_OtherGeneralCellModel]] = {
        
        var array2 = [ST_OtherGeneralCellModel]()
        
        let m0 = ST_OtherGeneralCellModel()
        m0.infoKey = "输入手机号无法登陆"
        array2.append(m0)
        
        let m01 = ST_OtherGeneralCellModel()
        m01.infoKey = "收不到验证码怎么办？"
        array2.append(m01)
        
        
        let m02 = ST_OtherGeneralCellModel()
        m02.infoKey = "使用微信登陆失败怎么办？"
        array2.append(m02)
        
        let m03 = ST_OtherGeneralCellModel()
        m03.infoKey = "使用QQ登陆失败怎么办？"
        array2.append(m03)
        
        let m04 = ST_OtherGeneralCellModel()
        m04.infoKey = "其他问题？"
        array2.append(m04)
        
        return [array2]
    }()
    
}

extension ST_RegistHelperListController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_array[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cellModel = self.data_array[indexPath.section][indexPath.item]
        
        if let m = cellModel as? ST_GeneralOfSwitchCellModel{
            
            let cell = collectionView.dequeueReusableCell(withClass: ST_GeneralOfSwitchCell.self, for: indexPath)
            
            cell.cellModel = m
            
            cell.info_label.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
            
            return cell
            
        }else{
            
            let cell = collectionView.dequeueReusableCell(withClass: ST_OtherGeneralCell.self, for: indexPath)
            
            cell.cellModel = cellModel
            
            cell.info_label.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
            
            cell.content_label.textColor = Color.init(hexString: "#999999")
            
            return cell
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if let url = url_array[safe:indexPath.row]  {
            let vc = ST_RegistStepHelperController()
            vc.url = url
            self.navigationController?.pushViewController(vc)
        }else{
            
            
            let vc = ST_TianReportController()
            
            vc.type = .regist
            
            self.navigationController?.pushViewController(vc)
        }
    }
    
}

class ST_RegistStepHelperController: ST_BTLightBarViewController,  WKNavigationDelegate, UIScrollViewDelegate {
    
    fileprivate let webView = WKWebView()
    
    fileprivate lazy var comebackBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        let str = "仍未解决？前往".withAttributes([Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.init(hex: 0xACB4C0)!)])
        
        let str1 = "意见反馈".withAttributes([Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.medium)),Attribute.textColor(UIColor.init(hex: 0xFEC207)!)])
        
        str.append(str1)
        
        control.setAttributedTitle(str, for: UIControl.State.normal)
        
        control.isHidden = true
        
        control.addTarget(self, action: #selector(feedbackEvent), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    var url:URL?
   
    var hiddenfeedback = false
    
    fileprivate var isURLLoaded = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let str = url?.absoluteString, str.contains("help_pay"){
            self.title_nav_btn.setTitleForAllStates("支付教程")
            right_nav_btn.setTitleForAllStates("人工客服")
            right_nav_btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
            right_nav_btn.setTitleColorForAllStates(UIColor.init(hex: 0x333333)!)
            right_nav_btn.snp.remakeConstraints { (make) in
                make.right.equalTo(own_navigation_bar).offset(-14)
                make.bottom.equalTo(own_navigation_bar)
                make.height.equalTo(44)
            }
            right_nav_btn.isHidden = false
        }else{
            self.title_nav_btn.setTitleForAllStates("问题详情")
        }
        
        
        webView.backgroundColor = Color.clear
//        webView.isOpaque = false
        webView.navigationDelegate = self
        
        webView.scrollView.delegate = self
        
        self.view.addSubview(webView)
     
        webView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.own_navigation_bar.snp.bottom)
        }
        
        self.view.addSubview(comebackBtn)
        comebackBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(20)
            make.bottom.equalToSuperview().offset(-20-BTSafeAreaBottom)
        }
        

        if let url = self.url{
            webView.load(URLRequest(url: url))
        }
        
    }
    
    override func right_click() {
        
        let pasteboard = UIPasteboard.general
        
        pasteboard.string = "bitiankefu"
        
         
        let alert = BTVerticalAlertViewController.createTianKeFuTalkAlert()
        
        alert.confirm_closure = {
            
            WXApi.openWXApp()
            
        }
        
        GlobalPresent(toController: alert)
    }
   
    
    @objc func feedbackEvent(){
        
        let vc = ST_TianReportController()
        
        vc.type = .regist
        
        self.navigationController?.pushViewController(vc)
    }
    
    
    // MARK: - delegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if isURLLoaded && !hiddenfeedback{
            if scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height) {
//                confirmButton.isEnabled = true
                comebackBtn.isHidden = false
            } else if scrollView.contentOffset.y < scrollView.contentSize.height {
//                confirmButton.isEnabled = false
                comebackBtn.isHidden = true
            }
        }
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        isURLLoaded = true
    }
}
